package edu.bit.water.service;

import edu.bit.water.dao.MetaBaselineDao;
import edu.bit.water.entity.MetaBaseline;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * MetaStandardController的service层
 * author: Page
 */
@Service
public class MetaBaselineService {

    @Autowired
    private MetaBaselineDao metaBaselineDao;

    /**
     * 查询MetaStandard
     */
    public List<MetaBaseline> queryBaseline(String stNumber) {
        return metaBaselineDao.queryBaseline(stNumber);
    }


    /**
     * 插入新的metaBaseline
     */
    public Map<String, Object> insertMetaBaselineService(MetaBaseline metaBaseline) {
        //metaBaseline.dateChange();
        Map<String, Object> result = new HashMap<>();
        Integer num = metaBaselineDao.getMaxId();
//        System.out.println(num);
        if (num != null) {
            metaBaseline.setId(num + 1);
        }
        else {
            metaBaseline.setId(0);
        }
        int tmp = metaBaselineDao.insertMetaBaseline(metaBaseline);
        if (tmp == 1) {
            result.put("status", "success");
        }else {
            result.put("status", "failed");
        }
        return result;
    }


    /**
     * 编辑提交
     */
    public Map<String, Object> updateMetaBaselineService(MetaBaseline metaBaseline) {
        //metaBaseline.dateChange();
        Map<String, Object> result = new HashMap<>();
        int tmp = metaBaselineDao.update(metaBaseline);
        if(tmp == 0) {
            result.put("status", "failed");
        }
        else {
            result.put("status", "success");
        }
        return result;
    }

    /**
     * 删除metaBaseline
     * author： page
     */
    public Map<String, Object> deleteMetaBaseline(List<String> metaBaseline) {
        Map<String, Object> tmpMap = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < metaBaseline.size(); i++) {
            int result = metaBaselineDao.delete(Integer.parseInt(metaBaseline.get(i)));
            if(result == 0) {
                tmpMap.put(metaBaseline.get(i), "failed");
                flag = false;
            }
            else {
                tmpMap.put(metaBaseline.get(i), "success");
            }
        }
        if (flag) {
            tmpMap.put("status", "success");
        }
        else {
            tmpMap.put("status", "failed");
        }
        return tmpMap;
    }
}
